﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.Composite.Extensions;
using Microsoft.Practices.Composite.Regions;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.LogViewer.Properties;

namespace Jbe.TestSuite.LogViewer.Applications
{
    internal class ModuleController
    {
        private readonly IApplicationContainer container;
        private readonly IRegionManager regionManager;
        private readonly IUIElementCreationService uiElementCreationService;
        private LogViewPresentationModel presentationModel;


        public ModuleController(IApplicationContainer container, IRegionManager regionManager, IUIElementCreationService uiElementCreationService)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.uiElementCreationService = uiElementCreationService;
        }


        public void Initialize()
        {
            IMenuItem menuItem = uiElementCreationService.CreateMenuItem(Resources.LogViewerMenu, Resources.EventLog.ToBitmap());
            menuItem.Click += MenuItemClick;
            regionManager.Regions[ShellRegionNames.ViewMenuItems].Add(menuItem);
            ShowLogViewer();
        }

        private void MenuItemClick(object sender, EventArgs e)
        {
            ShowLogViewer();
        }

        private void ShowLogViewer()
        {
            if (presentationModel == null)
            {
                presentationModel = container.Resolve<LogViewPresentationModel>();
                regionManager.Regions[ShellRegionNames.BottomRegion].Add(presentationModel.View);
            }
            regionManager.Regions[ShellRegionNames.BottomRegion].Activate(presentationModel.View);
        }
    }
}
